package yana.tpe.onlineshop.services.impl;

import java.util.List;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yana.tpe.onlineshop.domain.dao.ProductDao;
import yana.tpe.onlineshop.domain.domains.Category;
import yana.tpe.onlineshop.domain.domains.Product;
import yana.tpe.onlineshop.services.CategoryService;
import yana.tpe.onlineshop.services.ProductService;

/**
 *
 */
@Service
@Transactional
public class ProductServiceImpl extends AbstractServiceImpl<Integer, Product>
        implements ProductService {

    private static final Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);

    @Autowired
    private ProductDao productDao;

    @Autowired
    private CategoryService categoryService;

    @Override
    protected ProductDao getDao() {
        return productDao;
    }

    @Override
    public Product getWithImage(int id) {
        logger.debug("Try to get product [id={}] with image.", id);

        Product product = get(id);

        logger.debug("Product is [{}]", product);

        if (product != null) {
            logger.debug("Product image is [{}]", String.valueOf(product.getImage()));
        }

        return product;
    }

    @Override
    public Product getWithComments(int id) {
        logger.debug("Try to get product [id={}] with comments.", id);

        Product product = get(id);

        logger.debug("Product is [{}]", product);

        if (product != null) {
            logger.debug("Product comments is [{}]", product.getComments());
        }

        return product;
    }

    @Override
    public List<Product> getProductFromCategory(Category category) {
        logger.debug("getProductFromCategory({})", category);

        Integer categoryId = category != null ? category.getId() : null;
        List<Product> productFromCategory = getDao().getProductFromCategory(categoryId);

        logger.debug("{}", productFromCategory);

        return productFromCategory;
    }

    @Override
    public void updateProduct(@NotNull Product product, Integer categoryId) {
        logger.debug("Try to update product [{}], in category [id={}]", product, categoryId);

        Category category = categoryService.get(categoryId);

        logger.debug("Category is [{}]", category);

        product.setCategory(category);
        saveOrUpdate(product);
    }

    @Override
    public void addProduct(Integer categoryId, Product product) {
        logger.debug("Try to add product [{}], to category [id={}]", product, categoryId);

        Category category = categoryService.get(categoryId);

        logger.debug("Category is [{}]", category);

        product.setCategory(category);
        save(product);
    }

    @Override
    public void delete(Integer id) {
        logger.debug("Try to delete product [id={}]", id);
        getDao().delete(id);
    }
}
